package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Modelo.Perfil;

@Stateless(mappedName = "PerfilDAO")
public class PerfilDAO extends DAO {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<Perfil> listarPerfil() {
		List<Perfil> modelList = new ArrayList<Perfil>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(Perfil.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			modelList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return modelList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Perfil> listarPerfilSemAdministrador() {
		List<Perfil> modelList = new ArrayList<Perfil>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(Perfil.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.ne("nome", "Administrador"));
			modelList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return modelList;
	}
	
	public Perfil selecionarAdministradorCliente() {
		Perfil instance = new Perfil();
		try {
			begin();
			instance = (Perfil) this.session.createCriteria(Perfil.class)
					.add(Restrictions.eq("nome", "Administrador Cliente"))
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return instance;
	}
	
	public Perfil selecionarBasico() {
		Perfil instance = new Perfil();
		try {
			begin();
			instance = (Perfil) this.session.createCriteria(Perfil.class)
					.add(Restrictions.eq("nome", "Usuário"))
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return instance;
	}
	
	public Perfil selecionarAdministrador() {
		Perfil instance = new Perfil();
		try {
			begin();
			instance = (Perfil) this.session.createCriteria(Perfil.class)
					.add(Restrictions.eq("nome", "Administrador"))
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return instance;
	}
}